package com.ruipengkj;

import java.awt.Dimension;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JFrame;


public final class SwingUtil implements Serializable {
	private static final long serialVersionUID = 1L;
	private static ResourceBundle rb;
	public static void main(String[] args) throws Exception{
		 java.net.URL url = ClassLoader.getSystemResource("com/ruipengkj/images/list_bg.png");
		File file=new File(url.getFile());
		FileInputStream fis=new FileInputStream(file);
		System.out.println(file);
	}
	public static String getOpenSwingImagePath(String imageName){
		
		return "/com/ruipengkj/images/"+imageName;
	}
    public static ImageIcon getOpenSwingImage(String name) {
        return getOpenSwingImage(name, null);
    }
    
    /**
     * 取得图象资源
     * @param name String 图像名
     * @param defaultIcon ImageIcon 未取到时赋予的默认图像
     * @return ImageIcon
     */
    public static ImageIcon getOpenSwingImage(String name,ImageIcon defaultIcon) {
        ImageIcon icon = null;
        try {
            java.net.URL url = ClassLoader.getSystemResource("com/ruipengkj/images/" + name);
            icon = new ImageIcon(url);
        }
        catch (Exception ex) {
        }
        if (icon == null|| icon.getImageLoadStatus() != MediaTracker.COMPLETE|| icon.getIconHeight() <= 0) {
            icon = defaultIcon;
        }
        return icon;
    }
    /**
     * 取得国际化文字资源
     * @param key String  关键字
     * @return String
     */
    public static String getOpenResource(String key) {
        if (rb == null) {
            try{
                rb = ResourceBundle.getBundle("com.ruipengkj.ruipengkj",
                    Locale.getDefault(),
                    ClassLoader.getSystemClassLoader());
            }
            catch (Exception ex) {
                throw new NullPointerException("ERROR:CAN NOT FOUND RESOURCE FILE!  " +
                    new File("ruipengkj_" + Locale.getDefault().toString() +".properties").getAbsolutePath());
            }
        }
        return rb.getString(key);
    }

    public static JFrame createDemoFrame(String title){
        JFrame frame = new JFrame(title);
        frame.setSize(400, 320);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2,
                          (d.height - frame.getSize().height) / 2);
        return frame;
    }
}
